//
//  RGMeSexEditVC.m
//  RedGrass
//
//  Created by wuyine on 2018/12/14.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMeSexEditVC.h"

@implementation RGMeSexCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor blackColor];
        self .contentView.backgroundColor = [UIColor blackColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.title];
        [self.contentView addSubview:self.selectImg];
        
        [_title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(15);
            make.centerY.mas_equalTo(0);
        }];
        
        [_selectImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.mas_equalTo(-15);
            make.size.mas_equalTo(CGSizeMake(17, 12));
            make.centerY.mas_equalTo(0);
        }];
        
        _selectImg.hidden = YES;
    }
    return self;
}

//- (void)setSelected:(BOOL)selected {
//    if (selected) {
//        self.selectImg.hidden = NO;
//    }else {
//        self.selectImg.hidden = YES;
//    }
//}



- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
        _title.textColor = UICOLOR_WITH_RGBINT(0xffffff);
        _title.font = FONT(customFontName, 14);
    }
    return _title;
}

- (UIImageView *)selectImg {
    if (!_selectImg) {
        _selectImg = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"性别勾选")];
    }
    return _selectImg;
}
@end

#import "RGMeInterfaceBulider.h"
@interface RGMeSexEditVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSIndexPath *lastIndexPath;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation RGMeSexEditVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.customNavBar.title = @"性别";
    [self showBackButton];
    [self showRightButtonWithTitle:@"保存" target:self selector:@selector(save:)];
    self.customContentView.backgroundColor = [UIColor blackColor];
    [self.customContentView addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    NSInteger row = _originalIndex - 1;
    if( row < 0 || row > 2){
        row = 0;
    }
    self.lastIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
//    self.tableView.separatorColor = UICOLOR_WITH_RGBINT(0x999999);
}

- (void)save:(UIButton *)btn {
    if(self.userInfoChangedBlock){
        self.userInfoChangedBlock(self.lastIndexPath.row + 1);
    }
    [self.navigationController popViewControllerAnimated:YES];
//    HttpRequest *quest = [HttpRequest buildRequest:url_editInfo];
//    RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
//    user.sex = self.lastIndexPath.row;
//    NSDictionary *params = [RGMeInterfaceBulider buildEditInfo:user];
//    AA_WEAK_MAKE(self) weakSelf = self;
//    [quest post:params success:^(id data) {
//        [weakSelf dismissLoading];
//        [RGUserInfoModel storeUserInfo:user];
//        [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationUserInfoChangedSuccess object:nil];
//    } fail:^(NSInteger code, NSString *message) {
//        [weakSelf showTextMessageWithMessage:message];
//    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RGMeSexCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sex"];
    if(indexPath.row == _lastIndexPath.row){
        cell.selectImg.hidden = NO;
    }else{
        cell.selectImg.hidden = YES;
    }
    switch (indexPath.row) {
        case 0: {
            cell.title.text = @"男";
        }
            break;
        case 1: {
            cell.title.text = @"女";
        }
            break;
        case 2: {
            cell.title.text = @"不显示";
        }
            break;
        default:
            break;
    }
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    RGMeSexCell *cell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
//    cell.selectImg.hidden = YES;
//    RGMeSexCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
//    newCell.selectImg.hidden = NO;
    self.lastIndexPath = indexPath;
    [tableView reloadData];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = UICOLOR_WITH_RGBINT(0x000000);
        [_tableView registerClass:[RGMeSexCell class] forCellReuseIdentifier:@"sex"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    }
    return _tableView;
}
@end
